﻿
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GamePanel : MonoBehaviour
{
    public static GamePanel instance;
    public GameObject can, Obj;
    public int MonsterNum, BossNum, BossHp, MaxHp, MinHp, BulletSpeed, BulletHurt, Score, MaxScore;
    public GameObject Place0, Place1;
    public GameObject ZheZhao;
    public float t1,t3;
    public int i0, i4, FuHuo;
    public bool IsJieSuan, IsDeath, BossDeath;
    public Image JinDuTiaoImage;
    public Text JinDuText;
    public GameObject player;
    public Button BackButton;
    public bool YiFuHuo;
    


    /// <summary>
    /// 加分
    /// </summary>
    /// <param name="fen"></param>
    public void AddScore(int fen)
    {

        Score += fen;
        Debug.Log("Score===" + Score);
        if (Score == MonsterNum)
        {
            IsJieSuan = true;


        }
    }
    public void AddMaxScore(int fen)
    {
        MaxScore += fen;
    }




    void Start()
    {

        instance = this;
        Player.instance.IsMove = false;
        can = GameObject.Find("Canvas");
        player = GameObject.Find("Player");
        player.GetComponent<Image>().enabled = true;

        GetAllData(DataManager.Instance.Lv + 10000000);
        BackButton.onClick.AddListener(OnClickBackButton);


    }

   
    /// <summary>
    /// 更新怪物血量，数量，boss数量
    /// </summary>
    /// <param name="round"></param>
    public void GetAllData(int round)
    {


        LanguageCfgRowData data = DataManager.Instance.HLLanguageCfg.datas[round] as LanguageCfgRowData;
        MonsterNum = data.MonsterNum;
        Debug.Log("MonsterNum===" + MonsterNum);
        BulletSpeed = data.BulletSpeed;
        BulletHurt = data.BulletHurt;
        BossNum = data.BossNum;
        BossHp = data.BossHp;


    }





    /// <summary>
    /// 克隆怪物
    /// </summary>
    /// <param name="Monster"></param>
    public void CloneMonster(Balloon Monster)
    {
        GameObject obj = GameTools.instance.GetGameobject(Monster);
        obj.GetComponent<Monster>().IsFirstClone = false;
        obj.GetComponent<Monster>().Only = 0;


        if (BossNum == 0)
        {
            switch (Random.Range(0, 2))
            {
                case 0:
                    obj.transform.SetParent(Place0.transform);
                    obj.transform.localPosition = Vector3.zero;
                    obj.transform.forward = Place0.transform.forward;

                    break;
                case 1:
                    obj.transform.SetParent(Place1.transform);
                    obj.transform.localPosition = Vector3.zero;
                    break;
            }
        }
        else
        {
            obj.transform.SetParent(gameObject.transform);
            obj.transform.localPosition = Obj.transform.localPosition;

        }
    }
    /// <summary>
    /// 真正克隆怪物
    /// </summary>
    public void RealClone()
    {
        int a = Random.Range(0, 10);

        if (a < 5)
        {
            CloneMonster(Balloon.First);
        }
        else if (a >= 5 && a < 7)
        {
            CloneMonster(Balloon.Second);
        }
        else if (a >= 7 && a < 9)
        {
            CloneMonster(Balloon.Third);
        }
        else
        {
            CloneMonster(Balloon.Fourth);
        }
    }
    /// <summary>
    /// 克隆boss
    /// </summary>
    public void CloneBoss()
    {
        if (i4 < BossNum)
        {
            Obj = Instantiate(Resources.Load<GameObject>("Boss"));
            Obj.transform.SetParent(gameObject.transform);
            Obj.transform.localPosition = Vector3.zero + new Vector3(0, 400, 0);
            i4++;
        }
    }
    void Update()
    {
        if (!IsDeath)
        {
            if (i4 == 0)
            {
                t1 += Time.deltaTime;
                if (t1 > 5f)
                {
                    if (i0 < MonsterNum)
                    {
                        RealClone();
                        i0++;

                    }
                    t1 = 0;

                }
            }
            else
            {
                if (!BossDeath)
                {
                    t1 += Time.deltaTime;
                    if (t1 > 5.5f)
                    {
                        RealClone();
                        t1 = 0;
                    }
                }

            }
            CloneBoss();
        }
        if (IsJieSuan)
        {
            if (gameObject.transform.childCount == 9)
            {
                DataManager.Instance.Gold += MaxScore * 2;
                DataManager.Instance.Lv += 1;
                DataManager.Instance.Save();
                IsJieSuan = false;
                FuHuo = 0;
                Player.instance.IsMove = true;
                GameObject obj = Instantiate(Resources.Load<GameObject>("UIPanel/SuccessPanel"));
                obj.transform.SetParent(can.transform);
                obj.transform.localPosition = Vector3.zero;
                DestroyImmediate(gameObject);
                
            }
        }
        if (IsDeath)
        {
            ZheZhao.SetActive(true);
        }
        if (YiFuHuo)
        {
            
            t3 += Time.deltaTime;
            if (t3 >= 2f)
            {
                player.GetComponent<BoxCollider2D>().enabled = true;
                t3 = 0;
                YiFuHuo = false;

            }
           
        }


    }
    /// <summary>
    /// 普通关卡进度显示
    /// </summary>
    /// <param name="obj"></param>
    public void JinDu()
    {

        if (BossNum == 0)
        {
            JinDuTiaoImage.fillAmount += (float)1 / MonsterNum;
            JinDuText.text = (JinDuTiaoImage.fillAmount * 100).ToString("f0") + "%";
        }

    }
    /// <summary>
    /// /boss进度显示
    /// </summary>
    public void JinDuBoss()
    {
        JinDuTiaoImage.fillAmount += (float)MainPanel.instance.Hurt / BossHp;
        JinDuText.text = (JinDuTiaoImage.fillAmount * 100).ToString("f0") + "%";
    }
    public void OnClickBackButton()
    {
        ShortVoice.instance.PlayVoice("AnNiu");
        FuHuo = 0;
        Player.instance.IsMove = true;
        IsDeath = true;
        GameObject obj = Instantiate(Resources.Load<GameObject>("UIPanel/BackPanel"));
        obj.transform.SetParent(can.transform);
        obj.transform.localPosition = Vector3.zero;
       
    }
}
